home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / stencil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  22.2 KB  |  1,002 lines

  1. /* $Id: stencil.c,v 1.3 1997/02/27 19:58:35 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.2
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: stencil.c,v $
  26.  * Revision 1.3  1997/02/27 19:58:35  brianp
  27.  * don't try to clear stencil buffer if there isn't one
  28.  *
  29.  * Revision 1.2  1996/09/15 14:18:55  brianp
  30.  * now use GLframebuffer and GLvisual
  31.  *
  32.  * Revision 1.1  1996/09/13 01:38:16  brianp
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37.  
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "context.h"
  41. #include "dlist.h"
  42. #include "macros.h"
  43. #include "pb.h"
  44. #include "stencil.h"
  45. #include "types.h"
  46.  
  47.  
  48.  
  49. /*
  50.  * Return the address of a stencil buffer value given the window coords:
  51.  */
  52. #define STENCIL_ADDRESS(X,Y)  (ctx->Buffer->Stencil + ctx->Buffer->Width * (Y) + (X))
  53.  
  54.  
  55. void gl_ClearStencil( GLcontext *ctx, GLint s )
  56. {
  57.    if (INSIDE_BEGIN_END(ctx)) {
  58.       gl_error( ctx, GL_INVALID_OPERATION, "glClearStencil" );
  59.       return;
  60.    }
  61.    ctx->Stencil.Clear = (GLstencil) s;
  62. }
  63.  
  64.  
  65.  
  66. void gl_StencilFunc( GLcontext *ctx, GLenum func, GLint ref, GLuint mask )
  67. {
  68.    GLint maxref;
  69.  
  70.    if (INSIDE_BEGIN_END(ctx)) {
  71.       gl_error( ctx, GL_INVALID_OPERATION, "glStencilFunc" );
  72.       return;
  73.    }
  74.  
  75.    switch (func) {
  76.       case GL_NEVER:
  77.       case GL_LESS:
  78.       case GL_LEQUAL:
  79.       case GL_GREATER:
  80.       case GL_GEQUAL:
  81.       case GL_EQUAL:
  82.       case GL_NOTEQUAL:
  83.       case GL_ALWAYS:
  84.          ctx->Stencil.Function = func;
  85.          break;
  86.       default:
  87.          gl_error( ctx, GL_INVALID_ENUM, "glStencilFunc" );
  88.          return;
  89.    }
  90.  
  91.    maxref = (1 << STENCIL_BITS) - 1;
  92.    ctx->Stencil.Ref = CLAMP( ref, 0, maxref );
  93.    ctx->Stencil.ValueMask = mask;
  94. }
  95.  
  96.  
  97.  
  98. void gl_StencilMask( GLcontext *ctx, GLuint mask )
  99. {
  100.    if (INSIDE_BEGIN_END(ctx)) {
  101.       gl_error( ctx, GL_INVALID_OPERATION, "glStencilMask" );
  102.       return;
  103.    }
  104.    ctx->Stencil.WriteMask = (GLstencil) mask;
  105. }
  106.  
  107.  
  108.  
  109. void gl_StencilOp( GLcontext *ctx, GLenum fail, GLenum zfail, GLenum zpass )
  110. {
  111.    if (INSIDE_BEGIN_END(ctx)) {
  112.       gl_error( ctx, GL_INVALID_OPERATION, "glStencilOp" );
  113.       return;
  114.    }
  115.    switch (fail) {
  116.       case GL_KEEP:
  117.       case GL_ZERO:
  118.       case GL_REPLACE:
  119.       case GL_INCR:
  120.       case GL_DECR:
  121.       case GL_INVERT:
  122.          ctx->Stencil.FailFunc = fail;
  123.          break;
  124.       default:
  125.          gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
  126.          return;
  127.    }
  128.    switch (zfail) {
  129.       case GL_KEEP:
  130.       case GL_ZERO:
  131.       case GL_REPLACE:
  132.       case GL_INCR:
  133.       case GL_DECR:
  134.       case GL_INVERT:
  135.          ctx->Stencil.ZFailFunc = zfail;
  136.          break;
  137.       default:
  138.          gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
  139.          return;
  140.    }
  141.    switch (zpass) {
  142.       case GL_KEEP:
  143.       case GL_ZERO:
  144.       case GL_REPLACE:
  145.       case GL_INCR:
  146.       case GL_DECR:
  147.       case GL_INVERT:
  148.          ctx->Stencil.ZPassFunc = zpass;
  149.          break;
  150.       default:
  151.          gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
  152.          return;
  153.    }
  154. }
  155.  
  156.  
  157.  
  158. /* Stencil Logic:
  159.  
  160. IF stencil test fails THEN
  161.    Don't write the pixel (RGBA,Z)
  162.    Execute FailOp
  163. ELSE
  164.    Write the pixel
  165. ENDIF
  166.  
  167. Perform Depth Test
  168.  
  169. IF depth test passes OR no depth buffer THEN
  170.    Execute ZPass
  171.    Write the pixel
  172. ELSE
  173.    Execute ZFail
  174. ENDIF
  175.  
  176. */
  177.  
  178.  
  179.  
  180.  
  181. /*
  182.  * Apply the given stencil operator for each pixel in the span whose
  183.  * mask flag is set.
  184.  * Input:  n - number of pixels in the span
  185.  *         x, y - location of leftmost pixel in the span
  186.  *         oper - the stencil buffer operator
  187.  *         mask - array [n] of flag:  1=apply operator, 0=don't apply operator
  188.  */
  189. static void apply_stencil_op_to_span( GLcontext *ctx,
  190.                                       GLuint n, GLint x, GLint y,
  191.                       GLenum oper, GLubyte mask[] )
  192. {
  193.    GLint i;
  194.    GLstencil s, ref;
  195.    GLstencil wrtmask, invmask;
  196.    GLstencil *stencil;
  197.  
  198.    wrtmask = ctx->Stencil.WriteMask;
  199.    invmask = ~ctx->Stencil.WriteMask;
  200.    ref = ctx->Stencil.Ref;
  201.    stencil = STENCIL_ADDRESS( x, y );
  202.  
  203.    switch (oper) {
  204.       case GL_KEEP:
  205.          /* do nothing */
  206.          break;
  207.       case GL_ZERO:
  208.      if (invmask==0) {
  209.         for (i=0;i<n;i++) {
  210.            if (mask[i]) {
  211.           stencil[i] = 0;
  212.            }
  213.         }
  214.      }
  215.      else {
  216.         for (i=0;i<n;i++) {
  217.            if (mask[i]) {
  218.           stencil[i] = stencil[i] & invmask;
  219.            }
  220.         }
  221.      }
  222.      break;
  223.       case GL_REPLACE:
  224.      if (invmask==0) {
  225.         for (i=0;i<n;i++) {
  226.            if (mask[i]) {
  227.                   stencil[i] = ref;
  228.            }
  229.         }
  230.      }
  231.      else {
  232.         for (i=0;i<n;i++) {
  233.            if (mask[i]) {
  234.           s = stencil[i];
  235.           stencil[i] = (invmask & s ) | (wrtmask & ref);
  236.            }
  237.         }
  238.      }
  239.      break;
  240.       case GL_INCR:
  241.      if (invmask==0) {
  242.         for (i=0;i<n;i++) {
  243.            if (mask[i]) {
  244.           s = stencil[i];
  245.           if (s<0xff) {
  246.              stencil[i] = s+1;
  247.           }
  248.            }
  249.         }
  250.      }
  251.      else {
  252.         for (i=0;i<n;i++) {
  253.            if (mask[i]) {
  254.           /* VERIFY logic of adding 1 to a write-masked value */
  255.           s = stencil[i];
  256.           if (s<0xff) {
  257.              stencil[i] = (invmask & s) | (wrtmask & (s+1));
  258.           }
  259.            }
  260.         }
  261.      }
  262.      break;
  263.       case GL_DECR:
  264.      if (invmask==0) {
  265.         for (i=0;i<n;i++) {
  266.            if (mask[i]) {
  267.           s = stencil[i];
  268.           if (s>0) {
  269.              stencil[i] = s-1;
  270.           }
  271.            }
  272.         }
  273.      }
  274.      else {
  275.         for (i=0;i<n;i++) {
  276.            if (mask[i]) {
  277.           /* VERIFY logic of subtracting 1 to a write-masked value */
  278.           s = stencil[i];
  279.           if (s>0) {
  280.              stencil[i] = (invmask & s) | (wrtmask & (s-1));
  281.           }
  282.            }
  283.         }
  284.      }
  285.      break;
  286.       case GL_INVERT:
  287.      if (invmask==0) {
  288.         for (i=0;i<n;i++) {
  289.            if (mask[i]) {
  290.           s = stencil[i];
  291.           stencil[i] = ~s;
  292.            }
  293.         }
  294.      }
  295.      else {
  296.         for (i=0;i<n;i++) {
  297.            if (mask[i]) {
  298.           s = stencil[i];
  299.           stencil[i] = (invmask & s) | (wrtmask & ~s);
  300.            }
  301.         }
  302.      }
  303.      break;
  304.       default:
  305.          abort();
  306.    }
  307. }
  308.  
  309.  
  310.  
  311.  
  312. /*
  313.  * Apply stencil test to a span of pixels before depth buffering.
  314.  * Input:  n - number of pixels in the span
  315.  *         x, y - coordinate of left-most pixel in the span
  316.  *         mask - array [n] of flag:  0=skip the pixel, 1=stencil the pixel
  317.  * Output:  mask - pixels which fail the stencil test will have their
  318.  *                 mask flag set to 0.
  319.  * Return:  0 = all pixels failed, 1 = zero or more pixels passed.
  320.  */
  321. GLint gl_stencil_span( GLcontext *ctx,
  322.                        GLuint n, GLint x, GLint y, GLubyte mask[] )
  323. {
  324.    GLubyte fail[MAX_WIDTH];
  325.    GLint allfail = 0;
  326.    GLuint i;
  327.    GLstencil r, s;
  328.    GLstencil *stencil;
  329.  
  330.    stencil = STENCIL_ADDRESS( x, y );
  331.  
  332.    /*
  333.     * Perform stencil test.  The results of this operation are stored
  334.     * in the fail[] array:
  335.     *   IF fail[i] is non-zero THEN
  336.     *       the stencil fail operator is to be applied
  337.     *   ELSE
  338.     *       the stencil fail operator is not to be applied
  339.     *   ENDIF
  340.     */
  341.    switch (ctx->Stencil.Function) {
  342.       case GL_NEVER:
  343.          /* always fail */
  344.          for (i=0;i<n;i++) {
  345.         if (mask[i]) {
  346.            mask[i] = 0;
  347.            fail[i] = 1;
  348.         }
  349.         else {
  350.            fail[i] = 0;
  351.         }
  352.      }
  353.      allfail = 1;
  354.      break;
  355.       case GL_LESS:
  356.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  357.      for (i=0;i<n;i++) {
  358.         if (mask[i]) {
  359.            s = stencil[i] & ctx->Stencil.ValueMask;
  360.            if (r < s) {
  361.           /* passed */
  362.           fail[i] = 0;
  363.            }
  364.            else {
  365.           fail[i] = 1;
  366.           mask[i] = 0;
  367.            }
  368.         }
  369.         else {
  370.            fail[i] = 0;
  371.         }
  372.      }
  373.      break;
  374.       case GL_LEQUAL:
  375.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  376.      for (i=0;i<n;i++) {
  377.         if (mask[i]) {
  378.            s = stencil[i] & ctx->Stencil.ValueMask;
  379.            if (r <= s) {
  380.           /* pass */
  381.           fail[i] = 0;
  382.            }
  383.            else {
  384.           fail[i] = 1;
  385.           mask[i] = 0;
  386.            }
  387.         }
  388.         else {
  389.            fail[i] = 0;
  390.         }
  391.      }
  392.      break;
  393.       case GL_GREATER:
  394.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  395.      for (i=0;i<n;i++) {
  396.         if (mask[i]) {
  397.            s = stencil[i] & ctx->Stencil.ValueMask;
  398.            if (r > s) {
  399.           /* passed */
  400.           fail[i] = 0;
  401.            }
  402.            else {
  403.           fail[i] = 1;
  404.           mask[i] = 0;
  405.            }
  406.         }
  407.         else {
  408.            fail[i] = 0;
  409.         }
  410.      }
  411.      break;
  412.       case GL_GEQUAL:
  413.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  414.      for (i=0;i<n;i++) {
  415.         if (mask[i]) {
  416.            s = stencil[i] & ctx->Stencil.ValueMask;
  417.            if (r >= s) {
  418.           /* passed */
  419.           fail[i] = 0;
  420.            }
  421.            else {
  422.           fail[i] = 1;
  423.           mask[i] = 0;
  424.            }
  425.         }
  426.         else {
  427.            fail[i] = 0;
  428.         }
  429.      }
  430.      break;
  431.       case GL_EQUAL:
  432.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  433.      for (i=0;i<n;i++) {
  434.         if (mask[i]) {
  435.            s = stencil[i] & ctx->Stencil.ValueMask;
  436.            if (r == s) {
  437.           /* passed */
  438.           fail[i] = 0;
  439.            }
  440.            else {
  441.           fail[i] = 1;
  442.           mask[i] = 0;
  443.            }
  444.         }
  445.         else {
  446.            fail[i] = 0;
  447.         }
  448.      }
  449.      break;
  450.       case GL_NOTEQUAL:
  451.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  452.      for (i=0;i<n;i++) {
  453.         if (mask[i]) {
  454.            s = stencil[i] & ctx->Stencil.ValueMask;
  455.            if (r != s) {
  456.           /* passed */
  457.           fail[i] = 0;
  458.            }
  459.            else {
  460.           fail[i] = 1;
  461.           mask[i] = 0;
  462.            }
  463.         }
  464.         else {
  465.            fail[i] = 0;
  466.         }
  467.      }
  468.      break;
  469.       case GL_ALWAYS:
  470.      /* always pass */
  471.      for (i=0;i<n;i++) {
  472.         fail[i] = 0;
  473.      }
  474.      break;
  475.       default:
  476.          abort();
  477.    }
  478.  
  479.    apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.FailFunc, fail );
  480.  
  481.    return (allfail) ? 0 : 1;
  482. }
  483.  
  484.  
  485.  
  486.  
  487. /*
  488.  * Apply the combination depth-buffer/stencil operator to a span of pixels.
  489.  * Input:  n - number of pixels in the span
  490.  *         x, y - location of leftmost pixel in span
  491.  *         z - array [n] of z values
  492.  * Input:  mask - array [n] of flags  (1=test this pixel, 0=skip the pixel)
  493.  * Output:  mask - array [n] of flags (1=depth test passed, 0=failed) 
  494.  */
  495. void gl_depth_stencil_span( GLcontext *ctx,
  496.                             GLuint n, GLint x, GLint y, const GLdepth z[],
  497.                 GLubyte mask[] )
  498. {
  499.    if (ctx->Depth.Test==GL_FALSE) {
  500.       /*
  501.        * No depth buffer, just apply zpass stencil function to active pixels.
  502.        */
  503.       apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZPassFunc, mask );
  504.    }
  505.    else {
  506.       /*
  507.        * Perform depth buffering, then apply zpass or zfail stencil function.
  508.        */
  509.       GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
  510.       GLuint i;
  511.  
  512.       /* init pass and fail masks to zero, copy mask[] to oldmask[] */
  513.       for (i=0;i<n;i++) {
  514.      passmask[i] = failmask[i] = 0;
  515.          oldmask[i] = mask[i];
  516.       }
  517.  
  518.       /* apply the depth test */
  519.       (*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask );
  520.  
  521.       /* set the stencil pass/fail flags according to result of depth test */
  522.       for (i=0;i<n;i++) {
  523.          if (oldmask[i]) {
  524.             if (mask[i]) {
  525.                passmask[i] = 1;
  526.             }
  527.             else {
  528.                failmask[i] = 1;
  529.             }
  530.          }
  531.       }
  532.  
  533.       /* apply the pass and fail operations */
  534.       apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZFailFunc, failmask );
  535.       apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZPassFunc, passmask );
  536.    }
  537. }
  538.  
  539.  
  540.  
  541.  
  542. /*
  543.  * Apply the given stencil operator for each pixel in the array whose
  544.  * mask flag is set.
  545.  * Input:  n - number of pixels in the span
  546.  *         x, y - array of [n] pixels
  547.  *         operator - the stencil buffer operator
  548.  *         mask - array [n] of flag:  1=apply operator, 0=don't apply operator
  549.  */
  550. static void apply_stencil_op_to_pixels( GLcontext *ctx,
  551.                                         GLuint n, const GLint x[],
  552.                         const GLint y[],
  553.                         GLenum oper, GLubyte mask[] )
  554. {
  555.    GLint i;
  556.    GLstencil ref;
  557.    GLstencil wrtmask, invmask;
  558.  
  559.    wrtmask = ctx->Stencil.WriteMask;
  560.    invmask = ~ctx->Stencil.WriteMask;
  561.  
  562.    ref = ctx->Stencil.Ref;
  563.  
  564.    switch (oper) {
  565.       case GL_KEEP:
  566.          /* do nothing */
  567.          break;
  568.       case GL_ZERO:
  569.      if (invmask==0) {
  570.         for (i=0;i<n;i++) {
  571.            if (mask[i]) {
  572.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  573.                   *sptr = 0;
  574.            }
  575.         }
  576.      }
  577.      else {
  578.         for (i=0;i<n;i++) {
  579.            if (mask[i]) {
  580.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  581.           *sptr = invmask & *sptr;
  582.            }
  583.         }
  584.      }
  585.      break;
  586.       case GL_REPLACE:
  587.      if (invmask==0) {
  588.         for (i=0;i<n;i++) {
  589.            if (mask[i]) {
  590.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  591.                   *sptr = ref;
  592.            }
  593.         }
  594.      }
  595.      else {
  596.         for (i=0;i<n;i++) {
  597.            if (mask[i]) {
  598.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  599.           *sptr = (invmask & *sptr ) | (wrtmask & ref);
  600.            }
  601.         }
  602.      }
  603.      break;
  604.       case GL_INCR:
  605.      if (invmask==0) {
  606.         for (i=0;i<n;i++) {
  607.            if (mask[i]) {
  608.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  609.           if (*sptr < 0xff) {
  610.              *sptr = *sptr + 1;
  611.           }
  612.            }
  613.         }
  614.      }
  615.      else {
  616.         for (i=0;i<n;i++) {
  617.            if (mask[i]) {
  618.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  619.           if (*sptr<0xff) {
  620.              *sptr = (invmask & *sptr) | (wrtmask & (*sptr+1));
  621.           }
  622.            }
  623.         }
  624.      }
  625.      break;
  626.       case GL_DECR:
  627.      if (invmask==0) {
  628.         for (i=0;i<n;i++) {
  629.            if (mask[i]) {
  630.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  631.           if (*sptr>0) {
  632.              *sptr = *sptr - 1;
  633.           }
  634.            }
  635.         }
  636.      }
  637.      else {
  638.         for (i=0;i<n;i++) {
  639.            if (mask[i]) {
  640.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  641.           if (*sptr>0) {
  642.              *sptr = (invmask & *sptr) | (wrtmask & (*sptr-1));
  643.           }
  644.            }
  645.         }
  646.      }
  647.      break;
  648.       case GL_INVERT:
  649.      if (invmask==0) {
  650.         for (i=0;i<n;i++) {
  651.            if (mask[i]) {
  652.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  653.                   *sptr = ~*sptr;
  654.            }
  655.         }
  656.      }
  657.      else {
  658.         for (i=0;i<n;i++) {
  659.            if (mask[i]) {
  660.                   GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
  661.                   *sptr = (invmask & *sptr) | (wrtmask & ~*sptr);
  662.            }
  663.         }
  664.      }
  665.      break;
  666.       default:
  667.          abort();
  668.      return;
  669.    }
  670. }
  671.  
  672.  
  673.  
  674. /*
  675.  * Apply stencil test to an array of pixels before depth buffering.
  676.  * Input:  n - number of pixels in the span
  677.  *         x, y - array of [n] pixels to stencil
  678.  *         mask - array [n] of flag:  0=skip the pixel, 1=stencil the pixel
  679.  * Output:  mask - pixels which fail the stencil test will have their
  680.  *                 mask flag set to 0.
  681.  * Return:  0 = all pixels failed, 1 = zero or more pixels passed.
  682.  */
  683. GLint gl_stencil_pixels( GLcontext *ctx,
  684.                          GLuint n, const GLint x[], const GLint y[],
  685.              GLubyte mask[] )
  686. {
  687.    GLubyte fail[PB_SIZE];
  688.    GLstencil r, s;
  689.    GLuint i;
  690.    GLint allfail = 0;
  691.  
  692.    /*
  693.     * Perform stencil test.  The results of this operation are stored
  694.     * in the fail[] array:
  695.     *   IF fail[i] is non-zero THEN
  696.     *       the stencil fail operator is to be applied
  697.     *   ELSE
  698.     *       the stencil fail operator is not to be applied
  699.     *   ENDIF
  700.     */
  701.  
  702.    switch (ctx->Stencil.Function) {
  703.       case GL_NEVER:
  704.          /* always fail */
  705.          for (i=0;i<n;i++) {
  706.         if (mask[i]) {
  707.            mask[i] = 0;
  708.            fail[i] = 1;
  709.         }
  710.         else {
  711.            fail[i] = 0;
  712.         }
  713.      }
  714.      allfail = 1;
  715.      break;
  716.       case GL_LESS:
  717.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  718.      for (i=0;i<n;i++) {
  719.         if (mask[i]) {
  720.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  721.            s = *sptr & ctx->Stencil.ValueMask;
  722.            if (r < s) {
  723.           /* passed */
  724.           fail[i] = 0;
  725.            }
  726.            else {
  727.           fail[i] = 1;
  728.           mask[i] = 0;
  729.            }
  730.         }
  731.         else {
  732.            fail[i] = 0;
  733.         }
  734.      }
  735.      break;
  736.       case GL_LEQUAL:
  737.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  738.      for (i=0;i<n;i++) {
  739.         if (mask[i]) {
  740.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  741.            s = *sptr & ctx->Stencil.ValueMask;
  742.            if (r <= s) {
  743.           /* pass */
  744.           fail[i] = 0;
  745.            }
  746.            else {
  747.           fail[i] = 1;
  748.           mask[i] = 0;
  749.            }
  750.         }
  751.         else {
  752.            fail[i] = 0;
  753.         }
  754.      }
  755.      break;
  756.       case GL_GREATER:
  757.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  758.      for (i=0;i<n;i++) {
  759.         if (mask[i]) {
  760.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  761.            s = *sptr & ctx->Stencil.ValueMask;
  762.            if (r > s) {
  763.           /* passed */
  764.           fail[i] = 0;
  765.            }
  766.            else {
  767.           fail[i] = 1;
  768.           mask[i] = 0;
  769.            }
  770.         }
  771.         else {
  772.            fail[i] = 0;
  773.         }
  774.      }
  775.      break;
  776.       case GL_GEQUAL:
  777.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  778.      for (i=0;i<n;i++) {
  779.         if (mask[i]) {
  780.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  781.            s = *sptr & ctx->Stencil.ValueMask;
  782.            if (r >= s) {
  783.           /* passed */
  784.           fail[i] = 0;
  785.            }
  786.            else {
  787.           fail[i] = 1;
  788.           mask[i] = 0;
  789.            }
  790.         }
  791.         else {
  792.            fail[i] = 0;
  793.         }
  794.      }
  795.      break;
  796.       case GL_EQUAL:
  797.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  798.      for (i=0;i<n;i++) {
  799.         if (mask[i]) {
  800.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  801.            s = *sptr & ctx->Stencil.ValueMask;
  802.            if (r == s) {
  803.           /* passed */
  804.           fail[i] = 0;
  805.            }
  806.            else {
  807.           fail[i] = 1;
  808.           mask[i] = 0;
  809.            }
  810.         }
  811.         else {
  812.            fail[i] = 0;
  813.         }
  814.      }
  815.      break;
  816.       case GL_NOTEQUAL:
  817.      r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
  818.      for (i=0;i<n;i++) {
  819.         if (mask[i]) {
  820.                GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
  821.            s = *sptr & ctx->Stencil.ValueMask;
  822.            if (r != s) {
  823.           /* passed */
  824.           fail[i] = 0;
  825.            }
  826.            else {
  827.           fail[i] = 1;
  828.           mask[i] = 0;
  829.            }
  830.         }
  831.         else {
  832.            fail[i] = 0;
  833.         }
  834.      }
  835.      break;
  836.       case GL_ALWAYS:
  837.      /* always pass */
  838.      for (i=0;i<n;i++) {
  839.         fail[i] = 0;
  840.      }
  841.      break;
  842.       default:
  843.          abort();
  844.    }
  845.  
  846.    apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.FailFunc, fail );
  847.  
  848.    return (allfail) ? 0 : 1;
  849. }
  850.  
  851.  
  852.  
  853.  
  854. /*
  855.  * Apply the combination depth-buffer/stencil operator to a span of pixels.
  856.  * Input:  n - number of pixels in the span
  857.  *         x, y - array of [n] pixels to stencil
  858.  *         z - array [n] of z values
  859.  * Input:  mask - array [n] of flags  (1=test this pixel, 0=skip the pixel)
  860.  * Output:  mask - array [n] of flags (1=depth test passed, 0=failed) 
  861.  */
  862. void gl_depth_stencil_pixels( GLcontext *ctx,
  863.                               GLuint n, const GLint x[], const GLint y[],
  864.                   const GLdepth z[], GLubyte mask[] )
  865. {
  866.    if (ctx->Depth.Test==GL_FALSE) {
  867.       /*
  868.        * No depth buffer, just apply zpass stencil function to active pixels.
  869.        */
  870.       apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.ZPassFunc, mask );
  871.    }
  872.    else {
  873.       /*
  874.        * Perform depth buffering, then apply zpass or zfail stencil function.
  875.        */
  876.       GLubyte passmask[PB_SIZE], failmask[PB_SIZE], oldmask[PB_SIZE];
  877.       GLuint i;
  878.  
  879.       /* init pass and fail masks to zero */
  880.       for (i=0;i<n;i++) {
  881.      passmask[i] = failmask[i] = 0;
  882.          oldmask[i] = mask[i];
  883.       }
  884.  
  885.       /* apply the depth test */
  886.       (*ctx->Driver.DepthTestPixels)( ctx, n, x, y, z, mask );
  887.  
  888.       /* set the stencil pass/fail flags according to result of depth test */
  889.       for (i=0;i<n;i++) {
  890.          if (oldmask[i]) {
  891.             if (mask[i]) {
  892.                passmask[i] = 1;
  893.             }
  894.             else {
  895.                failmask[i] = 1;
  896.             }
  897.          }
  898.       }
  899.  
  900.       /* apply the pass and fail operations */
  901.       apply_stencil_op_to_pixels( ctx, n, x, y,
  902.                                   ctx->Stencil.ZFailFunc, failmask );
  903.       apply_stencil_op_to_pixels( ctx, n, x, y,
  904.                                   ctx->Stencil.ZPassFunc, passmask );
  905.    }
  906.  
  907. }
  908.  
  909.  
  910.  
  911. /*
  912.  * Return a span of stencil values from the stencil buffer.
  913.  * Input:  n - how many pixels
  914.  *         x,y - location of first pixel
  915.  * Output:  stencil - the array of stencil values
  916.  */
  917. void gl_read_stencil_span( GLcontext *ctx,
  918.                            GLuint n, GLint x, GLint y, GLubyte stencil[] )
  919. {
  920.    GLstencil *s;
  921.  
  922.    if (ctx->Buffer->Stencil) {
  923.       s = STENCIL_ADDRESS( x, y );
  924.       MEMCPY( stencil, s, n * sizeof(GLubyte) );
  925.    }
  926. }
  927.  
  928.  
  929.  
  930. /*
  931.  * Write a span of stencil values to the stencil buffer.
  932.  * Input:  n - how many pixels
  933.  *         x,y - location of first pixel
  934.  *         stencil - the array of stencil values
  935.  */
  936. void gl_write_stencil_span( GLcontext *ctx,
  937.                             GLuint n, GLint x, GLint y,
  938.                 const GLubyte stencil[] )
  939. {
  940.    GLstencil *s;
  941.  
  942.    if (ctx->Buffer->Stencil) {
  943.       s = STENCIL_ADDRESS( x, y );
  944.       MEMCPY( s, stencil, n * sizeof(GLubyte) );
  945.    }
  946. }
  947.  
  948.  
  949.  
  950. /*
  951.  * Allocate a new stencil buffer.  If there's an old one it will be
  952.  * deallocated first.  The new stencil buffer will be uninitialized.
  953.  */
  954. void gl_alloc_stencil_buffer( GLcontext *ctx )
  955. {
  956.    GLuint buffersize = ctx->Buffer->Width * ctx->Buffer->Height;
  957.  
  958.    /* deallocate current stencil buffer if present */
  959.    if (ctx->Buffer->Stencil) {
  960.       free(ctx->Buffer->Stencil);
  961.       ctx->Buffer->Stencil = NULL;
  962.    }
  963.  
  964.    /* allocate new stencil buffer */
  965.    ctx->Buffer->Stencil = (GLstencil *) malloc(buffersize * sizeof(GLstencil));
  966.    if (!ctx->Buffer->Stencil) {
  967.       /* out of memory */
  968.       ctx->Stencil.Enabled = GL_FALSE;
  969.       gl_error( ctx, GL_OUT_OF_MEMORY, "gl_alloc_stencil_buffer" );
  970.    }
  971. }
  972.  
  973.  
  974.  
  975.  
  976. /*
  977.  * Clear the stencil buffer.  If the stencil buffer doesn't exist yet we'll
  978.  * allocate it now.
  979.  */
  980. void gl_clear_stencil_buffer( GLcontext *ctx )
  981. {
  982.    if (ctx->Visual->StencilBits==0 || !ctx->Buffer->Stencil) {
  983.       /* no stencil buffer */
  984.       return;
  985.    }
  986.  
  987.    if (ctx->Scissor.Enabled) {
  988.       /* clear scissor region only */
  989.       GLint y;
  990.       GLint width = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
  991.       for (y=ctx->Buffer->Ymin; y<=ctx->Buffer->Ymax; y++) {
  992.          GLstencil *ptr = STENCIL_ADDRESS( ctx->Buffer->Xmin, y );
  993.          MEMSET( ptr, ctx->Stencil.Clear, width * sizeof(GLstencil) );
  994.       }
  995.    }
  996.    else {
  997.       /* clear whole stencil buffer */
  998.       MEMSET( ctx->Buffer->Stencil, ctx->Stencil.Clear,
  999.               ctx->Buffer->Width * ctx->Buffer->Height * sizeof(GLstencil) );
  1000.    }
  1001. }
  1002.